<template>
  <div class="main">
    <div class="form_content">
      <NScrollbar>
        <NSpin :show="spinShow">
          <div class="content">
            <VDisOrEnd v-if="props.title == '台账详情' ? false : RecordType.SealRouteName"
              :id="dispatchInfo.workflowInfoVo?.fromId" :assigneeFlag="dispatchInfo.workflowInfoVo?.assigneeFlag"
              :title="props.title == '台账详情' ? '台账申请单' : '用章申请单'"></VDisOrEnd>
            <div class="topTitle" v-else>
              {{ props.title }}
            </div>
            <!-- 用章详情 -->
            <div class="basin-info" v-if="props.type === '1'">
              <div class="basin-left">
                <div class="box">
                  <span>申请人部门：</span>
                  <span>{{ dispatchInfo.deptName }}</span>
                </div>
                <div class="box">
                  <span>申请时间：</span>
                  <span>{{ dispatchInfo.applicationTime }}</span>
                </div>
                <div class="box" v-if="isContractSeal()">
                  <span>用章名称：</span>
                  <span>{{ dispatchInfo.chapterCategoryName }}</span>
                </div>
                <div class="box" v-if="isCorporateSeal()">
                  <span>用章名称：</span>
                  <span>{{ dispatchInfo.chapterCategoryNames?.join(',') }}</span>
                </div>
                <div class="box" v-if="isCorporateSeal()">
                  <span>文件名称：</span>
                  <span>{{ dispatchInfo.docContractName }}</span>
                </div>
                <!-- 合同章内容 -->
                <template v-if="isContractSeal()">
                  <div class="box">
                    <span>合同名称：</span>
                    <span>{{ dispatchInfo.docContractName }}</span>
                  </div>
                  <div class="box">
                    <span>份数：</span>
                    <span>{{ dispatchInfo.chapterCategoryNum }}</span>
                  </div>
                  <div class="box" v-if="dispatchInfo.relatedMoney === '1'">
                    <span>收付款类型：</span>
                    <span>{{ renderMoneyType() }}</span>
                  </div>
                  <div class="box">
                    <span>合同履行开始日期：</span>
                    <span>{{ dispatchInfo.startTime }}</span>
                  </div>
                  <div class="box">
                    <span>主要内容：</span>
                    <span>{{ dispatchInfo.docContractName }}</span>
                  </div>
                </template>
                <div class="box" v-if="dispatchInfo.fileList?.length">
                  <div style="margin-bottom: auto;">
                    <span style="min-width: 90rem;">申请附件：</span>
                  </div>
                  <div style="display: flex;flex-wrap: wrap;gap: 2px 0;">
                    <VAttachments :attach-list="dispatchInfo.fileList"></VAttachments>
                  </div>
                </div>
              </div>
              <div class="basin-right">
                <div class="box">
                  <span>申请人：</span>
                  <span>{{ dispatchInfo.userName }}</span>
                </div>
                <div class="box">
                  <span>用章类型：</span>
                  <span>{{ dispatchInfo.chapterCategoryName }}</span>
                </div>
                <template v-if="isCorporateSeal()">
                  <div class="box">
                    <span>份数：</span>
                    <span>{{ dispatchInfo.chapterCategoryNum }}</span>
                  </div>
                  <div class="box">
                    <span>用途：</span>
                    <span>{{ dispatchInfo.useInfo }}</span>
                  </div>
                </template>
                <template v-if="isContractSeal()">
                  <div class="box">
                    <span>合同类型：</span>
                    <span>{{ renderContract() }}</span>
                  </div>
                  <div class="box">
                    <span>相对方名称：</span>
                    <span>{{ dispatchInfo.sideName }}</span>
                  </div>
                  <div class="box">
                    <span>是否涉及金额：</span>
                    <span>{{ renderRelatedMoney() }}</span>
                  </div>
                  <div class="box" v-if="dispatchInfo.relatedMoney === '1'">
                    <span>金额：</span>
                    <span>{{ dispatchInfo.contractAmount }}</span>
                  </div>
                  <div class="box">
                    <span>合同履行结束日期：</span>
                    <span>{{ dispatchInfo.endTime }}</span>
                  </div>
                </template>
                <div class="box" v-if="dispatchInfo.fileArchivingList?.length">
                  <div style="margin-bottom: auto;">
                    <span>归档附件：</span>
                  </div>
                  <div style="display: flex;flex-wrap: wrap;gap: 2px 0;">
                    <VAttachments :attach-list="dispatchInfo.fileArchivingList"></VAttachments>
                  </div>
                </div>
              </div>
            </div>
            <!-- 用章确认详情、台账详情 -->
            <div class="basin-info" v-else>
              <div class="basin-left">
                <div class="box">
                  <span>申请人部门：</span>
                  <span>{{ dispatchInfo.deptName }}</span>
                </div>
                <div class="box">
                  <span>申请时间：</span>
                  <span>{{ dispatchInfo.applicationTime }}</span>
                </div>
                <div class="box" v-if="isContractSeal()">
                  <span>用章名称：</span>
                  <span>{{ dispatchInfo.chapterCategoryName }}</span>
                </div>
                <div class="box" v-if="isCorporateSeal()">
                  <span>用章名称：</span>
                  <span>{{ dispatchInfo.chapterCategoryNames?.join(',') }}</span>
                </div>
                <div class="box" v-if="dispatchInfo.contractAmount">
                  <span>合同金额(元)：</span>
                  <span>{{ dispatchInfo.contractAmount }}</span>
                </div>
                <div class="box">
                  <span>合同/文件名称：</span>
                  <span>{{ dispatchInfo.docContractName }}</span>
                </div>

                <div class="box" v-if="dispatchInfo.fileList?.length">
                  <div style="margin-bottom: auto;">
                    <span style="min-width: 90rem;">申请附件：</span>
                  </div>
                  <div style="display: flex;flex-wrap: wrap;gap: 2px 0;">
                    <VAttachments :attach-list="dispatchInfo.fileList"></VAttachments>
                  </div>
                </div>
              </div>
              <div class="basin-right">
                <div class="box">
                  <span>申请人：</span>
                  <span>{{ dispatchInfo.userName }}</span>
                </div>
                <div class="box">
                  <span>用章类型：</span>
                  <span>{{ dispatchInfo.chapterCategoryName }}</span>
                </div>
                <div class="box">
                  <span>用章数量：</span>
                  <span>{{ dispatchInfo.chapterCategoryNum }}</span>
                </div>
                <div class="box">
                  <span>用途：</span>
                  <span>{{ dispatchInfo.useInfo }}</span>
                </div>
                <div class="box" v-if="dispatchInfo.oppositeUnit">
                  <span>对方单位：</span>
                  <span>{{ dispatchInfo.oppositeUnit }}</span>
                </div>
                <div class="box" v-if="dispatchInfo.fileArchivingList?.length">
                  <div style="margin-bottom: auto;">
                    <span>归档附件：</span>
                  </div>
                  <div style="display: flex;flex-wrap: wrap;gap: 2px 0;">
                    <VAttachments :attach-list="dispatchInfo.fileArchivingList"></VAttachments>
                  </div>
                </div>
              </div>
            </div>
            <div class="topTitle">审批信息</div>
            <div class="approve">
              <div class="box">
                <span>审批状态</span>
                <span style="font-size: 20rem;">{{ dispatchInfo.workflowInfoVo?.status }}</span>
              </div>
              <div class="box">
                <span>审批流程</span>
                <div style="color: #5fb17f;font-size: 20rem;font-weight: 500;">
                  <div style="display: inline;" v-for="(item, index) in dispatchInfo.workflowInfoVo?.workflowList"
                    :key="index">
                    <span :style="{ textDecoration: item.value ? 'underline' : 'none' }">
                      {{ item.key }}</span>
                    <span v-if="index != dispatchInfo.workflowInfoVo?.workflowList.length - 1">→</span>
                  </div>
                </div>
              </div>
            </div>
            <div class="topTitle">审批记录</div>
            <div class="table">
              <NDataTable :bordered="false" :columns="columns" :data="tableData" :pagination="false" striped
                :rowKey="rowKey" />
            </div>
          </div>
        </NSpin>
      </NScrollbar>
    </div>
  </div>
  <FixedAction>
    <div style="display: flex">
      <NSpace justify="start" style="flex: 0 0 42%">
        <NButton style="width: 78px; color: #5fb17f" ghost @click="goBack">
          <template #icon>
            <img :src="VBackImg" style="width: 25rem; height: 25rem" />
          </template>
          返回
        </NButton>
      </NSpace>

      <NSpace justify="center">
        <NButton style="width: 78px; color: #5fb17f" type="primary"
          v-if='dispatchInfo.status == 2 && userId == dispatchInfo.userId' ghost @click="handleFile">
          归档
        </NButton>
      </NSpace>

    </div>
  </FixedAction>

  <NModal :auto-focus="false" v-model:show="fileVisiable">
    <sealApplicationFile @close="fileVisiable = false" title="归档提示" @update="goBack" :value="modalId" />
  </NModal>
</template>

<script setup lang="ts">
import { NScrollbar, NDataTable, useMessage, NSpin, NSpace, NButton, NModal } from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
import { sendRequest } from '@/utils/api'
import VAttachments from '@/views/OA/Common/Attachments.vue'
import VDisOrEnd from '@/views/OA/Common/DisOrEnd.vue'
import FixedAction from '@/components/FixedAction.vue'
import VBackImg from '@/assets/images/back.png'
import router from '@/router'
import { useTagStore } from '@/stores/layout'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import sealApplicationFile from './SealApplicationFile.vue'
import { RecordType } from '@/router/utils'
import { SealApplication_ContractTypeList, SealApplication_RelatedMoney, SealApplication_MoneyTypeOptions } from '@/enums/index'

const route = useRoute()
const message = useMessage()
const tags = useTagStore().tags
const props = defineProps({
  type: { type: String }, // 1：用章详情，2：台账详情，3：用章确认详情
  title: { type: String, default: () => '用章详情' },
  id: { type: [Number, String], default: () => null }
})

const rowKey: any = (row: any) => row.id || ''
const columns: DataTableColumns = [
  {
    title: '类型',
    key: 'type',
    width: 50
  },
  {
    title: '人员',
    key: 'userName',
    width: 50
  },
  {
    title: '时间',
    key: 'time',
    width: 100
  },
  {
    title: '结果',
    key: 'result',
    width: 50
  },
  {
    title: '备注',
    key: 'remark',
    width: 150
  }
]
// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)

  tags.splice(index, 1)
  if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}
const renderContract = () => {
  if(!dispatchInfo.value.contract) {
    return '-'
  }
  const re = SealApplication_ContractTypeList.find(l => {
    return l.value === dispatchInfo.value.contract
  })
  return re ? re.label : '-'
}
const renderRelatedMoney = () => {
  if(!dispatchInfo.value.relatedMoney) {
    return '-'
  }
  const re = SealApplication_RelatedMoney.find(l => {
    return l.value === dispatchInfo.value.relatedMoney
  })
  return re ? re.label : '-'
}
const renderMoneyType = () => {
  if(!dispatchInfo.value.moneyType) {
    return '-'
  }
  const re = SealApplication_MoneyTypeOptions.find(l => {
    return l.value === dispatchInfo.value.moneyType
  })
  return re ? re.label : '-'
}
/** 是否是 合同章（原A0508） */
const isContractSeal = () => {
  if(!dispatchInfo || Object.keys(dispatchInfo).length === 0 || !(dispatchInfo as any).value.chapterCategoryCode) {
    return false
  }
  return ['A0560', 'A0561', 'A0562', 'A0563', 'A0564', 'A0565', 'A0566'].includes((dispatchInfo as any).value.chapterCategoryCode)
}
/** 是否是 法人章（原A0507） */
const isCorporateSeal = () => {
  if(!dispatchInfo || Object.keys(dispatchInfo).length === 0 || !(dispatchInfo as any).value.chapterCategoryCode) {
    return false
  }
  return (dispatchInfo as any).value.chapterCategoryCode === 'A0507'
}
// 获取详情
const spinShow: any = ref(false)
const dispatchInfo = ref<any>({})
const tableData: any = ref([])
const getReturnInfo = async () => {
  spinShow.value = true
  const [error, res] = await sendRequest({
    module: 'seal',
    api: 'sealApplicationItem',
    uri: {
      id: props?.id as never
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  dispatchInfo.value = res.data
  tableData.value = res.data?.workflowInfoVo?.approvalRecordsList || []
  spinShow.value = false
}

//获取用户信息
const userId: any = ref('')
const getUserInfo = async () => {
  const [error, res] = await sendRequest({
    module: 'seal',
    api: 'userInfo',
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  userId.value = res.data?.id
}
getUserInfo()

//归档
const fileVisiable = ref(false)
const modalId: any = ref()
function handleFile() {
  fileVisiable.value = true
  modalId.value = props.id || route.query.id
}

watch(
  () => route,
  async () => {
    if (
      (route.path == '/OA/Seal/SealApplicationDetail' ||
        route.path == '/OA/Seal/SealMakeSureDetail' ||
        route.path == '/OA/Seal/LedgerDetail') &&
      (props.id || route.query.id)
    ) {
      await getReturnInfo()
    }
  },
  { deep: true, immediate: true }
)
</script>

<style scoped lang="scss">
@mixin CommonFont {
  font-size: 18rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #333333;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .form_content {
    width: 100%;
    height: 100%;
    padding: 20rem 20rem 0;

    .topTitle {
      font-size: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      color: #5fb17f;
      margin-bottom: 10rem;
    }

    .content {
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 18rem 33rem 120rem;
      overflow-y: scroll;
      background-color: #ebeceb;
      @include CommonFont;

      .basin-info {
        display: flex;
        width: 100%;
        height: auto;
        border-bottom: 1px dashed #bc9e70;
        margin-bottom: 10rem;

        .basin-left {
          flex: 0 0 40%;
        }

        .box {
          display: flex;
          align-items: center;
          margin-bottom: 10rem;
          font-size: 16rem;

          span:first-child {
            display: inline-block;
            width: 120rem;
            font-weight: bold;
            color: #333333;
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
          }

          span:last-child {
            display: inline-block;
            flex: 1;
            color: #55524f;
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;

          }
        }

        .box:last-child {
          margin-bottom: 20rem;
        }
      }

      .approve {
        display: flex;
        width: 100%;
        margin-bottom: 20rem;
        border-bottom: 1px dashed #bc9e70;

        .box {
          display: flex;
          flex-direction: column;
          justify-content: center;
          min-width: 100rem;
          width: auto;
          height: 90rem;
          border-radius: 4px;
          border: 1px solid #d9d9d9;
          padding: 12rem;
          margin-right: 10rem;
          margin-bottom: 20rem;
        }
      }

      .table {
        width: 80%;
        background-color: #e1e1e1;
      }
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
